package day11;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Xiao
 * @date 2021/7/31
 */
public class Test05 {
    /**
     * 5. 定义任意字符串数组
     *     统计该数组中所有字符出现的次数，将统计结果输出到当前模块下 count.txt文件中
     *     举例：
     *     String[] str = {"hello","world","java","heiheihei"};
     *     count.txt:
     *         h:4
     *         e:4
     *         l:3
     *         o:2
     *         w:1
     *         r:1
     *         d:1
     *         j:1
     *         a:2
     *         i:3
     *         v:1
     *     提示： 在输出的内容后面加入：\r\n 就有换行效果
     */
    public static void main(String[] args) throws IOException {
        String[] str = {"hello","world","java","heiheihei"};
        FileOutputStream fos = new FileOutputStream("homework\\sr\\day11\\count.txt");
//        HashMap<Byte, Integer> hm = new HashMap<>();
        HashMap<Byte, Integer> hm = new LinkedHashMap<>();
        for (String s : str) {
            byte[] bytes = s.getBytes();
            for (byte b : bytes) {
                if (hm.containsKey(b)) {
                    int i = hm.get(b);
                    hm.put(b, i + 1);
                } else {
                    hm.put(b, 1);
                }
            }
        }
        for (Map.Entry<Byte, Integer> entry : hm.entrySet()) {
            /*String s = String.valueOf(entry.getValue());
            byte[] byt1 = {entry.getKey(), ':'};
            byte[] byt2 = s.getBytes();
            fos.write(byt1);
            fos.write(byt2);
            fos.write("\r\n".getBytes());*/
            //            System.out.println(s);
            fos.write(entry.getKey());
            fos.write((":" + entry.getValue() + "\r\n").getBytes());
        }
        fos.close();
    }
}
